package com.elong.businesstravel.base.basefragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dialog.CustomWaitDialog;
import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.utils.DateUtils;
import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.base.utils.SaveUserOperate;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BaseFragment extends Fragment {

	protected Context mParentContext;
	protected Fragment mFragment;

	protected ELongBusinessTravelApplication mApplication;

	private CustomWaitDialog mCustomWaitDialog;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mParentContext = this.getActivity();
		mFragment = this;
		mApplication = (ELongBusinessTravelApplication) mParentContext
				.getApplicationContext();

		DebugLog.setTag(mFragment.getClass().getName());
		// HandlerToastUI.getHandlerToastUI(mParentContext,
		// mFragment.getClass().getName());
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	protected void setViewData(View view) {

	}

	protected void setListener(View view) {

	}

	protected void setTitleNavBar(View view) {

	}

	/**
	 * 显示等待对话框
	 * 
	 * @param messageString
	 * @param onDismissListener
	 */
	protected void showWaitDialog(String messageString,
			final OnKeyCancel onKeyCancel) {
		if (null == mCustomWaitDialog) {
			mCustomWaitDialog = new CustomWaitDialog(mParentContext,
					com.android.support.jhf.R.style.CustomWaitDialog);
			mCustomWaitDialog.setCancelable(true);
			mCustomWaitDialog.setCanceledOnTouchOutside(false);
			mCustomWaitDialog.setMessage(messageString);
			mCustomWaitDialog.setOnKeyCancelListener(new OnKeyCancel() {

				@Override
				public void onKeyCancelListener() {
					if (null != onKeyCancel) {
						onKeyCancel.onKeyCancelListener();
					}
					if (null != mCustomWaitDialog) {
						mCustomWaitDialog.cancel();
						mCustomWaitDialog = null;
					}
				}
			});
			mCustomWaitDialog.show();
		}
	}

	protected void hideWaitDialog() {
		if (null != mCustomWaitDialog) {
			mCustomWaitDialog.cancel();
			mCustomWaitDialog = null;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		// 时间改变广播
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_TIME_TICK);
		mParentContext.registerReceiver(mTimeTickBroadcastReceiver, filter);
	}

	@Override
	public void onPause() {
		super.onPause();
		mParentContext.unregisterReceiver(mTimeTickBroadcastReceiver);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (null != savedInstanceState) {
			DebugLog.logI("Activity onRestoreInstanceState");
		}
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {

		DebugLog.logI("Fragment onSaveInstanceState");

		super.onSaveInstanceState(outState);
	}

	private final BroadcastReceiver mTimeTickBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_TIME_TICK)) {
				// HandlerToastUI.getHandlerToastUI(mContext,
				// "mTimeTickBroadcastReceiver" + mContext.getClass());

				String currentDateString = DateUtils.getCurrentDate("yyyyMMdd");
				String startDateString = DateUtils.dateFormat(
						mApplication.mHotelDestineDateStrings[0],
						"yyyy-MM-dd (E)", "yyyyMMdd");

				Date currentDate = new Date();
				currentDate.setTime(DateUtils.getDateMillis(currentDateString,
						"yyyyMMdd"));

				Date startDate = new Date();
				startDate.setTime(DateUtils.getDateMillis(startDateString,
						"yyyyMMdd"));

				if (currentDate.compareTo(startDate) != 0) {
					// HandlerToastUI.getHandlerToastUI(mContext,
					// "currentDate != startDate");
					mApplication.factoryDate();
					refreshDate();
				}
			}
		}
	};

	/**
	 * 每次都需要刷新搜索条件的日期
	 */
	private void refreshDate() {
		// 从缓存中取
		List<Date> mSaveDates = SaveUserOperate
				.getInOutHotelDates(mParentContext);
		checkInOutHotelDates(mSaveDates);
		// 设置默认
		mApplication.mHotelSearchCondition.mSearchConditionDate = mApplication.mHotelDestineDateStrings[0];
		mApplication.mHotelSearchCondition.mSearchConditionDays = "1晚";
		// 缓存有效设置缓存
		if (null != mSaveDates && 0 != mSaveDates.size()) {
			mApplication.mHotelSearchCondition.mSearchConditionDate = DateUtils
					.dateFormat(mSaveDates.get(0).getTime(), "yyyy-MM-dd (E)");
			mApplication.mHotelSearchCondition.mSearchConditionDays = (mSaveDates
					.size() - 1) + "晚";
		}
		// 更新显示
		String dateString = DateUtils.dateFormat(DateUtils.getDateMillis(
				mApplication.mHotelSearchCondition.mSearchConditionDate,
				"yyyy-MM-dd (E)"), "MM/dd");
		// mDateTextView.setText(dateString + " 住"
		// + mApplication.mHotelSearchCondition.mSearchConditionDays);
		refreshDateUI(dateString);
	}

	/**
	 * 核对从缓存中获取的时间
	 */
	private void checkInOutHotelDates(List<Date> mSaveDates) {
		if (null != mSaveDates && mSaveDates.size() > 0) {
			Date startSaveDate = mSaveDates.get(0);
			String startSaveDateString = DateUtils.dateFormat(
					startSaveDate.getTime(), "yyyyMMdd");
			startSaveDate.setTime(DateUtils.getDateMillis(startSaveDateString,
					"yyyyMMdd"));

			Date endSaveDate = mSaveDates.get(mSaveDates.size() - 1);
			String endSaveDateString = DateUtils.dateFormat(
					endSaveDate.getTime(), "yyyyMMdd");
			endSaveDate.setTime(DateUtils.getDateMillis(endSaveDateString,
					"yyyyMMdd"));

			String startHotel = mApplication.mHotelDestineDateStrings[0];
			Date startHotelDate = new Date(DateUtils.getDateMillis(startHotel,
					"yyyy-MM-dd (E)"));

			String endHotel = mApplication.mHotelDestineDateStrings[mApplication.mHotelDestineDateStrings.length - 1];
			Date endHotelDate = new Date(DateUtils.getDateMillis(endHotel,
					"yyyy-MM-dd (E)"));

			if (startHotelDate.compareTo(startSaveDate) > 0
					|| endHotelDate.compareTo(endSaveDate) < 0) {
				mSaveDates.clear();
				SaveUserOperate.setInOutHotelDates(mParentContext,
						new ArrayList<Date>());
			} else {

			}

		}
	}

	/**
	 * 刷新时间UI
	 */
	protected void refreshDateUI(String showDateUIString) {

	}

}
